﻿//	Copyright (c) 2003-2009 Olivier Giulieri - olivier@evolutility.org 

//	This file is part of Evolutility CRUD Framework.
//	Source link <http://www.evolutility.org/download/download.aspx>

//	Evolutility is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.

//	Evolutility is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.

//	You should have received a copy of the GNU General Public License
//	along with Evolutility. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Text;
using System.Data;
using System.Xml;
using System.Web;
using System.Drawing;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Web.Security; 
//using System.Data.SqlClient;

namespace Evolutility
{
    partial class UIServer //  Grid : for list and edit
    {

        private void WriteLog(string msg, string Categorie)
        {
            //创建一个日志条目
            LogEntry log = new LogEntry();
            //指定该日志所属类别
            log.Categories.Add(Categorie);
            //日志标题
            if (Membership.GetUser() != null)
                log.Title = Membership.GetUser().UserName;
            else
                log.Title = "System";
            log.Message = msg;
            //优先级
            log.Priority = 0;
            try
            {
            Logger.Write(log);
            }
            catch(Exception ex)
            {
                //TODO:....
            }
        }
        private void WriteDBActionLog(DatabaseEventArgs e)
        {
            string msg=string.Empty;
            string Categorie = "General";
            StringBuilder sbOldValues = new StringBuilder();
            StringBuilder sbNewValues = new StringBuilder();

            if (!string.IsNullOrEmpty(_Text))
            {


                switch (e.Action)
                {
                    case DBAction.INSERT:
                        msg = string.Format("插入记录在“{0}”中，ID为{1}。",_Text,e.ID);
                        break;
                    case DBAction.DELETE:
                        if (e.OldValues != null)
                        {
                            foreach (KeyValuePair<string, string> kvp in e.OldValues)
                            {
                                sbOldValues.AppendFormat("[{0}]=[{1}]， ",
                                    kvp.Key, kvp.Value);
                            }

                        }
                        msg = string.Format("删除了在“{0}”中ID为{1}的记录。原始的值是：{2}删除成功！", _Text, e.ID, sbOldValues.ToString());
                        break;
                    case DBAction.UPDATE:
                        if (e.OldValues != null)
                        {
                            foreach (KeyValuePair<string, string> kvp in e.OldValues)
                            {
                                sbOldValues.AppendFormat("[{0}]=[{1}]， ",
                                    kvp.Key, kvp.Value);
                            }

                        }
                        if (e.NewValues != null)
                        {
                            foreach (KeyValuePair<string, string> kvp in e.NewValues)
                            {
                                sbNewValues.AppendFormat("[{0}]=[{1}]， ",
                                    kvp.Key, kvp.Value);
                            }
                        }


                        msg = string.Format("修改了在“{0}”中ID为 {1} 的记录。原始的值是：{2}新的值是：{3}，更新成功！", _Text, e.ID,sbOldValues.ToString(),sbNewValues.ToString());

                        break;
                    case DBAction.DB_ERROR:
                        msg = string.Format("操作在“{0}”中，ID为{1}记录时出错！", _Text, e.ID);
                        Categorie = "Error";
                        break;
                }

                WriteLog(msg, Categorie);
            }
        }
        private void WriteGeneralLog(string msg)
        {
            WriteLog(msg, "General");
        }
        private void WriteLogForSQL(string sql)
        {
            WriteLog(sql, "SQLScript");
        }

        private void WriteLogByUser(string msg, string Categorie)
        {
            if (Membership.GetUser() != null)
                return;
            else
                WriteLog(msg, Categorie);
        }
    }
}
